﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Activation;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace Core
{
    public class WcfServiceHost
    {
        internal static ServiceHost m_ServiceHost = null;
        internal static string baseaddr = "http://localhost:8001/SmartHouseService/";

        public void StartSrvice(DataAccess.DataAccessor dep)
        {
            
            Uri baseAddress =   new Uri(baseaddr);
            SmartHouseWcfService.SmartHouseWcfService SHWCFService = new SmartHouseWcfService.SmartHouseWcfService();
            SHWCFService.DataAccessor = dep;
            m_ServiceHost = new ServiceHost(typeof(SmartHouseWcfService.SmartHouseWcfService), baseAddress);
            ServiceEndpoint ep = m_ServiceHost.AddServiceEndpoint(typeof(SmartHouseWcfService.ISmartHouseWCF), new WSHttpBinding(), "");

            // Enable metadata publishing.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            m_ServiceHost.Description.Behaviors.Add(smb);

            m_ServiceHost.Open();
        }

        public void StopService()
        {
            if (m_ServiceHost.State != CommunicationState.Closed)
            {
                m_ServiceHost.Close();
            }
        }
    }
    /*
    public class MyServiceHostFactory : ServiceHostFactory
    {
        private readonly DataAccess.DataAccessor dep;

        public MyServiceHostFactory()
        {
            this.dep = new DataAccess.DataAccessor();
        }

        protected override ServiceHost CreateServiceHost(Type serviceType,
            Uri[] baseAddresses)
        {
            return new MyServiceHost(this.dep, serviceType, baseAddresses);
        }
    }

    public class MyServiceHost : ServiceHost
    {
        public MyServiceHost(DataAccess.DataAccessor dep, Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
            if (dep == null)
            {
                throw new ArgumentNullException("dep");
            }

            foreach (var cd in this.ImplementedContracts.Values)
            {
                cd.Behaviors.Add(new MyInstanceProvider(dep));
            }
        }
    }

    public class MyInstanceProvider : IInstanceProvider, IContractBehavior
    {
        private readonly DataAccess.DataAccessor dep;

        public MyInstanceProvider(DataAccess.DataAccessor dep)
        {
            if (dep == null)
            {
                throw new ArgumentNullException("dep");
            }

            this.dep = dep;
        }

        #region IInstanceProvider Members

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return this.GetInstance(instanceContext);
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return new WcfService.SmartHouseWcfService(this.dep);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }

        #endregion

        #region IContractBehavior Members

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            dispatchRuntime.InstanceProvider = this;
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
        }

        #endregion
    }
    */
}

//

